<?php
/**
 * Created by lzl
 * Date: 2021 2021/10/11
 * Time: 13:20
 */
namespace App\Http\Controllers\Api;

use App\Forms\UserBehaviorForms;
use App\Forms\UserForms;
use App\Services\Api\AuthServices;
use App\Services\Api\UserServices;
use App\Services\Api\WithdrawServices;

class UserController extends Controller
{

    /**
     * @return mixed
     * @throws \App\Exceptions\RequestException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function login()
    {
        $rules = [
            'code'     => 'required',
            'source'   => 'nullable', //来源
        ];

        $this->validateInput($rules);

        return $this->success(AuthServices::login($this->validated));
    }

    /**
     * 主页
     * @return mixed
     */
    public function center()
    {
        return $this->success((new UserServices())->center());
    }

    /**
     * 个人资料
     * @return mixed
     */
    public function userMsg()
    {
        return $this->success((new UserServices())->userDetail());
    }


    /**
     * 修改用户资料
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function editUserMsg()
    {
        $rules = [
            'avatar'    => 'required|string',
            'nick_name' => 'required|string',
            'mobile'    => 'nullable',
            'sex'       => 'nullable',
            'birthday'  => 'nullable',
            'p_openid' =>  'nullable', //邀请人openid
            'source'   =>  'nullable', //注册渠道
        ];

        $this->validateInput($rules);

        return $this->success((new UserServices())->editUserMsg($this->validated));

    }


    /**
     * 添加用户行为
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function addBehavior()
    {
        $rules = [
            'goods_id' => 'required|integer',
            'type'     => 'required|in:1, 2' //1：浏览 2：收藏
        ];

        $this->validateInput($rules);

        return $this->successOrFailed((new UserServices())->addUserBehavior($this->validated));
    }


    /**
     * 用户行为列表
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function behaviorList()
    {
        $rules = [
            'page'  => 'required|integer',
            'limit' => 'required|integer',
            'type'  => 'required|in:1,2'
        ];

        $this->validateInput($rules);

        return $this->successOrNodata((new UserServices())->getBehaviorList($this->validated));

    }


    /**
     * 批量删除用户行为
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function deleteBehavior()
    {
        $rules = [
            'ids'  => 'required|array',
        ];

        $this->validateInput($rules);

        return $this->successOrFailed((new UserServices())->deleteBehavior($this->validated));
    }

    /**
     * 收货地址列表
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function addressList()
    {
        $rules = [
            'page'  => 'required|integer',
            'limit' => 'required|integer',
        ];

        $this->validateInput($rules);

        return $this->successOrNodata((new UserServices())->addressList($this->validated));
    }


    /**
     * 修改收货地址
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function editAddress()
    {
        $rules = [
            'id'         => 'nullable',
            'province'   => 'nullable',
            'city'       => 'nullable',
            'area'       => 'nullable',
            'details'    => 'nullable',
            'phone'      => 'nullable',
            'name'       => 'nullable',
            'is_default' => 'required|integer|in:0,1'
        ];

        $this->validateInput($rules);

        return $this->successOrFailed((new UserServices())->editAddress($this->validated));
    }


    /**
     * 删除收货地址
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function deleteAddress()
    {
        $rules = [
            'id' => 'required|integer'
        ];

        $this->validateInput($rules);

        return $this->successOrFailed((new UserServices())->deleteAddress($this->validated));
    }


    /**
     * 分享二维码
     * @return mixed
     */
    public function shareCodeList()
    {
        return $this->success((new UserServices())->getShareCode());
    }


    /**
     * @return mixed
     * @throws \App\Exceptions\RequestException
     * @throws \EasyWeChat\Kernel\Exceptions\DecryptException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getUserPhone()
    {
        $rules = [
            'code'          => 'required|string',
            'iv'            => 'required|string',
            'encryptedData' => 'required|string',
        ];

        $this->validateInput($rules);

        return $this->success(AuthServices::getUserPhone($this->validated));
    }


    /**
     * 用户提现
     * @return mixed
     * @throws \App\Exceptions\RequestException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Throwable
     */
    public function withdraw()
    {
        $rules = [
            'amount' => 'required|numeric',
        ];

        $this->validateInput($rules);

        return $this->success((new WithdrawServices())->send($this->validated));
    }
}